<?php
class model_tip extends base_model{
	public function __construct() {
		//默认不用登录
		$this->usercheck = false;
		parent::__construct();
	}
	/*
	首页购买方式数据汇总，存储过程中每30秒计算一次
	*/
	public function mod_ordersum(){
		//汇总数据
		$this->jsonObj['state'] = 200;
		
		$this->jsonObj['baina'] = intval($this->config['bainacoin']);
		$this->jsonObj['nil'] = intval($this->config['nilcoin']);
		$this->jsonObj['buy'] = intval($this->config['buycoin']);
		return $this->jsonObj;
	}
	// 私信提醒
	public function mod_message() {
		$this->check_post();
		$this->user_check_func();
		$uid = $this->user['uid'];
		$this->jsonObj['message'] = $this->language['post_error'];
		$temp = $this->db->query("SELECT `id` FROM `@@message_receiver` WHERE `to_uid`={$uid} AND `is_deleted`=0 AND `is_readed`=0 AND `flag`=0 ORDER BY `id` DESC", 3);
		if (count($temp)>0) {
			$ids = array();
			foreach ($temp as $v){
				array_push($ids, $v['id']);
			}
			$cookie = array_key_exists('message', $_COOKIE) && !empty($_COOKIE['message']) ? explode(',', $_COOKIE['message']) : array();
			if (array_diff($ids, $cookie)) {
				$id = $ids[0];
				$sql = "UPDATE `@@message_receiver` SET `flag`=1 WHERE `id`={$id} LIMIT 1";
				$this->db->query($sql, 0);
				setcookie('message', implode(',', $ids));
				$this->jsonObj['state'] = 200;
				$this->jsonObj['message'] = $this->language['have_message'];
			}
		}
		
	}
	
	// 充值或提现提示
	public function mod_money() {
		$this->check_post();
		$this->user_check_func();
		$uid = $this->user['uid'];
		// 提现提示
		$sql = "SELECT `id` FROM `@@member_cash` WHERE (`state`=0 OR `state`=4) AND `isDelete`=0 AND `flag`=0 AND `uid`=$uid ORDER BY `id` DESC";
		$temp = $this->db->query($sql, 3);
		//var_dump(count($temp));
		if (count($temp)>0) {
			$ids = array();
			foreach ($temp as $v){
				array_push($ids, $v['id']);
			}
			$cookie = array_key_exists('money_cash', $_COOKIE) && !empty($_COOKIE['money_cash']) ? explode(',', $_COOKIE['money_cash']) : array();
			if (array_diff($ids, $cookie)) {
				$id = $ids[0];
				$this->db->query("UPDATE `@@member_cash` SET `flag`=1 WHERE `id`=$id LIMIT 1", 0);
				$data = $this->db->query("SELECT `amount`,`state`,`info` FROM `@@member_cash` WHERE `id`=$id LIMIT 1", 2);
				$amount = $data['amount'];
				$state = $data['state'];
				$info = $data['info'];
				setcookie('money_cash', implode(',', $ids));
				if ($state == 4) {
					$type = 'error';
					$text = '您的提现请求被驳回，原因：'.$info;
				} else {
					$type = 'success';
					$text = '您的提现请求已处理，金额<span class="btn btn-red">'.$amount.'</span>元';
				}
				$this->dialogue(array(
					'type' => $type,
					'text' => $text,
					'auto' => true,
					'yes'  => array(
						'text' => '我知道了',
					),
					'no'  => array(
						'text' => '查看提现记录',
						'func' => 'setTimeout("location.href=\'/user/cashlog\';",300);',
					),
				));
				exit;
			}
		}
		// 充值提示
		$sql = "SELECT `id` FROM `@@member_recharge` WHERE (`state`=1 OR `state`=9) AND `isDelete`=0 AND `flag`=0 AND `uid`=$uid ORDER BY `id` DESC";
		$temp = $this->db->query($sql, 3);
		if (count($temp)>0) {
			$ids = array();
			foreach ($temp as $v) array_push($ids, $v['id']);
			$cookie = array_key_exists('money_recharge', $_COOKIE) && !empty($_COOKIE['money_recharge']) ? explode(',', $_COOKIE['money_recharge']) : array();
			if (array_diff($ids, $cookie)) {
				$id = $ids[0];
				$this->db->query("UPDATE `@@member_recharge` SET `flag`=1 WHERE `id`=$id LIMIT 1", 0);
				$data = $this->db->query("SELECT CASE WHEN `state`=9 THEN `amount` ELSE `rechargeAmount` END `CZAmount` FROM `@@member_recharge` WHERE `id`=$id LIMIT 1", 2);
				$CZAmount = $data['CZAmount'];
				setcookie('money_recharge', implode(',', $ids));
				if ($CZAmount > 0) {
					$text = '您的充值请求已处理，金额<span class="btn btn-red">'.$CZAmount.'</span>元';
					$no = array(
						'text' => '查看充值记录',
						'func' => 'setTimeout("location.href=\'/user/rechargelog\';",300);',
					);
				} else {
					$text = '您的账户已被扣款<span class="btn btn-red">'.abs($CZAmount).'</span>元，如有疑问请与管理员联系';
					$no = null;
				}
				$this->dialogue(array(
					'type' => 'success',
					'text' => $text,
					'auto' => true,
					'yes'  => array(
						'text' => '我知道了',
					),
					'no' => $no,
				));
				exit;
			}
		}
	}

}
?>